package com.cslg.questionnaires.service;

import com.cslg.questionnaires.entity.Question;
import com.cslg.questionnaires.mapper.QuestionMapper;
import com.cslg.questionnaires.utils.QuestionUtils;
import com.cslg.questionnaires.utils.ServiceMessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@Service
public class EditQuestionService {
    @Resource
    QuestionMapper questionMapper;

    /**
     * 测试是否能修改
     * @param questionId
     * @param userId
     * @return
     */
    public boolean canQuestionEdit(int questionId,int userId,String userType){
        Integer flag = questionMapper.getExamineState(questionId,userId,userType);
        if (flag == 8 || flag == 9)
            return true;
        else
            return false;
    }

    public HashMap<String,Object> getEditQuesiton(int questionId){
        HashMap<String, Object> data = new HashMap<>();
        Question editQuestion = questionMapper.getQuestionById(questionId);
        data.put("id",editQuestion.getId());
        data.put("title",editQuestion.getTitle());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date start = editQuestion.getStarttime();
        Date finish = editQuestion.getFinishtime();
        String starttime = sdf.format(start).replace(" ","T");
        String finishtime = sdf.format(finish).replace(" ","T");
        data.put("starttime",starttime);
        data.put("finishtime",finishtime);
        ArrayList content = QuestionUtils.parseQuestionContent(editQuestion.getContent());
        data.put("content",content);
        return ServiceMessageBuilder
                .buildMessage(200,"success",data);
    }

    public HashMap<String, Object> saveEditQuestion(HashMap<String,Object> map, int questionId,int owerid){
        Question questionFrom;
        try {
            questionFrom = QuestionUtils.buildQuestionForm(map,owerid);
        } catch (ParseException e) {
            return ServiceMessageBuilder
                    .buildMessage(500,e.toString(),null);
        }
        questionFrom.setId(questionId);
        questionFrom.setIsexamine(8);
        int row = questionMapper.editQuestion(questionFrom);
        if (row == 1){
            return ServiceMessageBuilder
                    .buildMessage(200,"success",null);
        } else {
            return ServiceMessageBuilder
                    .buildMessage(502,"error",null);
        }
    }

    public HashMap<String, Object> publishEditQuestion(HashMap<String,Object> map, int questionId,int owerid){
        Question questionFrom;
        try {
            questionFrom = QuestionUtils.buildQuestionForm(map,owerid);
        } catch (ParseException e) {
            return ServiceMessageBuilder
                    .buildMessage(500,e.toString(),null);
        }
        questionFrom.setId(questionId);
        questionFrom.setIsexamine(1);
        int row = questionMapper.editQuestion(questionFrom);
        if (row == 1){
            return ServiceMessageBuilder
                    .buildMessage(200,"success",null);
        } else {
            return ServiceMessageBuilder
                    .buildMessage(502,"error",null);
        }
    }

}
